package com.ecommerce.product;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileUtil {
    // 存储文件路径（可根据实际环境调整）
    private static final String FILE_PATH = "products.txt";

    // 方法1：将商品列表写入文件（存储功能）
    public static void saveProducts(List<Product> products) throws IOException {
        // 使用BufferedWriter写入文件，处理String类型的商品信息
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {
            for (Product product : products) {
                String line;
                // 区分商品类型，存储特有属性（便于后续读取时识别子类）
                if (product instanceof ElectronicProduct) {
                    ElectronicProduct electronic = (ElectronicProduct) product;
                    line = "E," + electronic.getProductId() + "," + electronic.getProductName() + "," +
                            electronic.getPrice() + "," + electronic.getDescription() + "," +
                            electronic.getBrand() + "," + electronic.getBatteryLife();
                } else if (product instanceof ClothingProduct) {
                    ClothingProduct clothing = (ClothingProduct) product;
                    line = "C," + clothing.getProductId() + "," + clothing.getProductName() + "," +
                            clothing.getPrice() + "," + clothing.getDescription() + "," +
                            clothing.getSize() + "," + clothing.getMaterial();
                }else if (product instanceof BookProduct) {
                    BookProduct book = (BookProduct) product;
                    line = "B," + book.getProductId() + "," + book.getProductName() + "," +
                            book.getPrice() + "," + book.getDescription() + "," +
                            book.getAuthor() + "," + book.getPublisher() + "," + book.getIsbn();
                } else {
                    line = "P," + product.getProductId() + "," + product.getProductName() + "," +
                            product.getPrice() + "," + product.getDescription() + ",,";
                }
                writer.write(line);
                writer.newLine(); // 换行存储
            }
        }
    }

    // 方法2：从文件读取商品信息，解析为Product列表（读取功能）
    public static List<Product> readProducts() throws IOException, InvalidPriceException {
        List<Product> products = new ArrayList<>();
        File file = new File(FILE_PATH);
        if (!file.exists()) {
            return products; // 文件不存在时返回空列表
        }

        // 使用BufferedReader读取文件，解析String为商品对象
        try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(","); // 按逗号分割属性
                if (parts.length < 5) {
                    continue; // 数据格式错误时跳过
                }

                Product product = null;
                // 根据首字符识别商品类型，创建对应子类对象
                switch (parts[0]) {
                    case "E": // 电子产品
                        if (parts.length >= 7) {
                            product = new ElectronicProduct(
                                    parts[1], parts[2], Double.parseDouble(parts[3]),
                                    parts[4], parts[5], Integer.parseInt(parts[6])
                            );
                        }
                        break;
                    case "C": // 服装
                        if (parts.length >= 7) {
                            product = new ClothingProduct(
                                    parts[1], parts[2], Double.parseDouble(parts[3]),
                                    parts[4], parts[5], parts[6]
                            );
                        }
                        break;
                    case "B": // 图书
                        if (parts.length >= 8) {
                            product = new BookProduct(
                                    parts[1], parts[2], Double.parseDouble(parts[3]),
                                    parts[4], parts[5], parts[6], parts[7]
                            );
                        }
                        break;
                    case "P": // 基础商品（可选）
                        product = new Product(parts[1], parts[2], Double.parseDouble(parts[3]), parts[4]);
                        break;
                }
                if (product != null) {
                    products.add(product);
                }
            }
        }
        return products;
    }
}